package chan.module.project.hw.mrs.hive2.v350.test;

import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

public class HiveDataGenerator {
    private static final Random random = new Random();
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private static final SimpleDateFormat timestampFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static void main(String[] args) {
        String name = "t.aaa.name";
        System.out.println(name.substring( name.lastIndexOf(".") + 1 ));
    }


    public static void main1(String[] args) {
        try (FileWriter writer = new FileWriter("C:\\Users\\taher\\Desktop\\北明-中建-dxp\\测试\\hive_test_1w_data.csv")) {
            // 生成1000行数据
            for (int i = 0; i < 10000; i++) {
                writer.write(generateRow() + "\n");
            }
            System.out.println("数据生成完成，保存至 hive_data_1000rows.csv");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String generateRow() {
        // 生成随机日期和时间戳
        Date randomDate = randomDate();
        Date randomTimestamp = new Date(randomDate.getTime() + random.nextInt(86400000));

        return String.join(",",
                String.valueOf(random.nextInt(256) - 128),  // TINYINT (-128~127)
                String.valueOf(random.nextInt(65536) - 32768),  // SMALLINT (-32768~32767)
                String.valueOf(random.nextInt()),  // INT
                String.valueOf(random.nextLong()),  // BIGINT
                String.valueOf(random.nextBoolean()),  // BOOLEAN
                String.valueOf(random.nextFloat() * 100),  // FLOAT
                String.valueOf(random.nextDouble() * 10000),  // DOUBLE
                String.format("%.2f", random.nextDouble() * 100000),  // DECIMAL(10,2)
                "str_" + randomString(10),  // STRING
                "var_" + randomString(random.nextInt(20) + 1),  // VARCHAR(255)
                String.format("%-10s", "char_" + random.nextInt(100)),  // CHAR(10)
                dateFormat.format(randomDate),  // DATE
                timestampFormat.format(randomTimestamp)  // TIMESTAMP
        );
    }

    private static Date randomDate() {
        long minDay = new Date(70, 0, 1).getTime();
        long maxDay = new Date(125, 11, 31).getTime();
        return new Date(ThreadLocalRandom.current().nextLong(minDay, maxDay));
    }

    private static String randomString(int length) {
        String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append(chars.charAt(random.nextInt(chars.length())));
        }
        return sb.toString();
    }
}

